
#include <windows.h>
#include <windowsx.h>
#include <xpdefines.h>
#include <xpdiv.h>

int xDPos = 0, yDPos = 0;
int xUPos = 0, yUPos = 0;
int xPos = 0, yPos = 0;
int isDown = 0;
int isToDrawAll = 1;
char *AppTitle="Win1";

xpdiv *g_pDiv;

void xpdiv_init(HWND hwnd)
{
		RECT rcClient; 
		GetClientRect(hwnd, &rcClient); 
		
		g_pDiv = xpdiv_createx(rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
		xpdiv_rect rct;
		rct.x = 0;
		rct.y = 0;
		rct.w = 100;
		rct.h = 200;
		xpdiv_rect_insert(g_pDiv, &rct);
		xpdiv_rect_occupy(g_pDiv, &rct);
}

void xpdiv_repaint(HDC hdc)
{
		xpdiv_point *xref;
		xpdiv_point *yref;
		

		HPEN hPen = CreatePen(PS_DOT,1,RGB(0,255,0));
    HPEN hPenOld = SelectObject(hdc, hPen);

		/*Drawo hor line*/	
		yref = g_pDiv->root;
		while (yref != NULL)
		{
			xref = yref;
			MoveToEx(hdc, xref->x, xref->y, NULL);
			while (xref->xnext != NULL)
				xref = xref->xnext;
		  LineTo(hdc, xref->x, xref->y);
			yref = yref->ynext;
		}
		
		xref = g_pDiv->root;
		while (xref != NULL)
		{
			yref = xref;
			MoveToEx(hdc, yref->x, yref->y, NULL);
			while (yref->ynext != NULL)
				yref = yref->ynext;
		  LineTo(hdc, yref->x, yref->y);
			xref = xref->xnext;
		}
		
   	SelectObject(hdc, hPenOld);
		DeleteObject(hPen);

		
		RECT rct;
		/*draw block*/
		yref = g_pDiv->root;
		while (yref != NULL)
		{
				xref = yref;
				while (xref != NULL)
				{
					if (xref != NULL
						&& !xref->isAvail
						&& xref->xnext != NULL
						&& xref->xnext->ynext != NULL)
						{
							rct.left = xref->x;
							rct.top = xref->y;
							rct.right = xref->xnext->ynext->x;
							rct.bottom = xref->xnext->ynext->y;
							FillRect(hdc, &rct, GetStockObject(BLACK_BRUSH));
						}
					xref = xref->xnext;	
				}
				yref = yref->ynext;
		}
		
		/*draw first available*/
		xpdiv_rect xprct;
		xpdiv_rect_get(g_pDiv, &xprct);
		rct.left = xprct.x;
		rct.top = xprct.y;
		rct.right = xprct.x + xprct.w;
		rct.bottom = xprct.y + xprct.h;
		FillRect(hdc, &rct, GetStockObject(GRAY_BRUSH));
		
		hPen = CreatePen(PS_DOT,5,RGB(0, 0, 0));
    hPenOld = SelectObject(hdc, hPen);
		xpdiv_rect_used(g_pDiv, &xprct);
		MoveToEx(hdc, xprct.x, xprct.y, NULL);
		LineTo(hdc, xprct.x + xprct.w, xprct.y);
		LineTo(hdc, xprct.x + xprct.w, xprct.y + xprct.h);
		LineTo(hdc, xprct.x, xprct.y + xprct.h);
		LineTo(hdc, xprct.x, xprct.y);

		SelectObject(hdc, hPenOld);
		DeleteObject(hPen);



}

LRESULT WINAPI MsgProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	HDC hdc;
PAINTSTRUCT ps;
RECT rcClient; 
char buf[128];
 HFONT font;
 xpdiv_rect xprct;
 RECT rct;

    switch( msg )
    {
        case WM_DESTROY:
            
            PostQuitMessage( 0 );
            return 0;
				case WM_PAINT:
		
	
					
					memset((void *)buf, 0, 128);
					sprintf((void *)buf, "x: %d, y: %d", xPos, yPos);
					hdc=BeginPaint(hwnd,&ps);
						xpdiv_repaint(hdc);
					TextOut(hdc,10,10, buf, 20);
					if (isDown)
					  Rectangle(hdc, xDPos, yDPos, xPos, yPos); 
				  EndPaint(hwnd,&ps);					
					break;            
				case WM_LBUTTONUP:
					xPos = xUPos = GET_X_LPARAM(lParam);
					yPos = yUPos = GET_Y_LPARAM(lParam);
					InvalidateRect(hwnd, NULL, TRUE); 
					isDown = 0;
					if (xDPos > xUPos)
				  {
						 xprct.x = xUPos;
						 xprct.w = xDPos - xUPos;
					}
					else
						{
							xprct.x = xDPos;
							xprct.w = xUPos - xDPos;
						}
						
					if (yDPos > yUPos)
				  {
						 xprct.y = yUPos;
						 xprct.h = yDPos - yUPos;
					}
					else
						{
							xprct.y = yDPos;
							xprct.h = yUPos - yDPos;
						}
						
				 xpdiv_rect_insert(g_pDiv, &xprct);
				 xpdiv_rect_occupy(g_pDiv, &xprct);
				 isToDrawAll = 1;
					break;					
				case WM_LBUTTONDOWN:
					xPos = xDPos = GET_X_LPARAM(lParam);
					xPos = yDPos = GET_Y_LPARAM(lParam);
					isDown = 1;
					break;				
				case WM_MOUSEMOVE:
					if (isDown)
					{
						rct.left = xDPos; rct.top = yDPos;
						rct.right = xPos; rct.bottom =  yPos;
						InvalidateRect(hwnd, &rct, TRUE); 										
					}

					xPos = GET_X_LPARAM(lParam);
					yPos = GET_Y_LPARAM(lParam);
					if (isDown)
					{
						rct.left = xDPos; rct.top = yDPos;
						rct.right = xPos; rct.bottom =  yPos;
						InvalidateRect(hwnd, &rct, TRUE); 										
					}
					break;						

    }

    return DefWindowProc( hwnd, msg, wParam, lParam );
}

int WINAPI WinMain (HINSTANCE hInstance, 
HINSTANCE hPrevInstance, 
PSTR szCmdLine, 
int iCmdShow) 
{

 WNDCLASS wc; 
  HWND hwnd; 
  MSG msg; 

//  wc.style=CS_HREDRAW | CS_VREDRAW; 
  wc.style=CS_CLASSDC; 
  wc.lpfnWndProc=MsgProc; 
  wc.cbClsExtra=0; 
  wc.cbWndExtra=0; 
  wc.hInstance=hInstance; 
  wc.hIcon=LoadIcon(NULL,IDI_WINLOGO); 
  wc.hCursor=LoadCursor(NULL,IDC_ARROW); 
  wc.hbrBackground=(HBRUSH)COLOR_WINDOWFRAME; 
  wc.lpszMenuName=NULL; 
  wc.lpszClassName=AppTitle; 

  if (!RegisterClass(&wc)) 
    return 0; 

  hwnd = CreateWindow(AppTitle,AppTitle, 
    WS_OVERLAPPEDWINDOW, 
    0, 0,1280,800, 
    NULL,NULL,hInstance,NULL); 

  if (!hwnd) 
    return 0; 
xpdiv_init(hwnd);
            // Show the window
            ShowWindow( hwnd, SW_SHOWDEFAULT );
            UpdateWindow( hwnd );

            // Enter the message loop

            ZeroMemory( &msg, sizeof( msg ) );
            while( msg.message != WM_QUIT )
            {
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
            }
        
    


return (0);

}
